package com.wy.OneClient.thread;

import com.wy.OneClient.Info;
import com.wy.OneClient.client;
import com.wy.OneClient.pojo.COMMAND;
import com.wy.OneClient.pojo.Data;
import com.wy.OneClient.pojo.DataType;
import com.wy.OneClient.pojo.Message;
import com.wy.OneClient.util.CommandWindowColorUtils;
import com.wy.OneClient.util.Utils;

import java.io.*;
import java.net.DatagramSocket;
import java.util.Scanner;

/**
 * @author 吴宇~
 * @motto 我亦无他，唯手熟尔。
 * @edition jdk1.8
 * @reflection 0-1 喧闹任其喧闹，自由我自为之，我自风情万种，与世无争。
 * @date 2024/03/03/18:50
 * @target 心若有所向往，何惧道阻且长
 */
//UDP 发送信息的线程
public class Send extends Thread {
    Scanner scanner;
    private DatagramSocket datagramSocket;
    //    我发送给你信息，你的端口和ip是多少
    private Message message;

    public Send(Scanner scanner, DatagramSocket datagramSocket, Message message) {
        this.scanner = scanner;
        this.datagramSocket = datagramSocket;
        this.message = message;
    }

    @Override
    public void run() {
        init();
        Utils.commandPrintGreen("发送端准备完成~");
        String sendMessage;
        while (client.notice) {
            try {
                //回复一条消息
                sendMessage = scanner.nextLine();
                if (sendMessage == null || "".equals(sendMessage)) {
                    continue;
                }
                checkSendMessage(sendMessage);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.err.println("发送端线程结束");
    }

    public void init() {
        Thread.currentThread().setName("sendThread");
        message.setData(new Data<String>(COMMAND.COMMAND_CONNECT_TARGET_MACHINE, DataType.COMMAND));
        try {
            Utils.sendMessageServiceImpl(datagramSocket, message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void checkSendMessage(String sendMessage) throws IOException {
        switch (sendMessage) {
//            TODO：接收端或者  另一端不能关闭连接。
            case COMMAND.COMMAND_CLOSE:
                client.notice = false;
                client.virtualConnectFlag = false;
                message.setData(new Data<String>(COMMAND.COMMAND_CLOSE, DataType.COMMAND));
                Utils.sendMessageServiceImpl(datagramSocket, message);
                break;
            case COMMAND.COMMAND_TO_CLOSE:
                Utils.helpInfo();
                break;
            case COMMAND.COMMAND_FILE:
                File file = new File(Info.DEFAULT_SEND_FILE);
                if (!file.exists()) {
                    CommandWindowColorUtils.printRed("文件路径有误：" + " " + Info.DEFAULT_SEND_FILE);
                    break;
                }

                Data<byte[]> fileData = new Data<>();
                byte[] byteArray = getByteArray(file.listFiles()[0]);
                fileData.setData(byteArray);
                fileData.setDataType(DataType.FILE);
//                开发发送文件
                message.setData(fileData);
                Utils.sendMessageServiceImpl(datagramSocket,message);
                break;
            default:
                message.setData(new Data<String>(sendMessage, DataType.STRING));
                Utils.sendMessageServiceImpl(datagramSocket, message);
        }
    }

    public byte[] getByteArray(File file) {
        FileInputStream fileInputStream = null;
        ByteArrayOutputStream bos;
        int bytesRead;
        byte[] fileBytes = null;
        byte[] bytes = new byte[1024];
        try {
            fileInputStream = new FileInputStream(file);
            bos = new ByteArrayOutputStream();
            while ((bytesRead = fileInputStream.read(bytes)) != -1) {
                bos.write(bytes, 0, bytesRead);
            }
            fileBytes = bos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return fileBytes;
    }
}
